//
// Created by yinlijun on 2017/9/30.
//

#include <stdint.h>
#include <malloc.h>
#include "work_data.h"
#include "../utils/app_sys_log.h"

struct WorkData {
    void* data;
    void* ctx;
    work_handler handler;
    work_data_destroy_func destroy;
};

WorkData* work_data_create(void* data, work_handler handler, work_data_destroy_func destroy, void* ctx) {
    if(handler == NULL) {
       return NULL;
    }
    WorkData* thiz = (WorkData*)malloc(sizeof(WorkData));
    thiz->handler = handler;
    thiz->destroy = destroy;
    thiz->data = data;

    return thiz;
}

void* work_data_get_data(WorkData* thiz) {
    return thiz == NULL ? NULL : thiz->data;
}

void* work_data_handle(WorkData* thiz) {
    return thiz->handler(thiz->data, thiz->ctx);
}

void work_data_destroy(WorkData* thiz) {
    //app_sys_warn("destory data");
    if(thiz->destroy) {
        thiz->destroy(thiz->data, thiz->ctx);
    }
    free(thiz);
}